﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Assets.Scripts.Bvh.Common
{
    public struct RadixTreeNode
    {
        public int parentIndex;
        public int nodeIndex;
        public int leftNodeIndex;
        public int rightNodeIndex;
        public int mortonCode;
        public int visitCount;

        public AABB aabb;

        public Triangle triangle;
        public int triangleValid;

        //初始化数据
        public readonly static RadixTreeNode Default = new RadixTreeNode()
        {
            parentIndex = -1,
            leftNodeIndex = -1,
            rightNodeIndex = -1,
            nodeIndex = -1,
            mortonCode = 0,
            aabb = AABB.Invalid,
            triangle = Triangle.Invalid,
            triangleValid = 0,
        };

        //Debug 字符串
        public override string ToString()
        {
            return $"NodeIndex:{nodeIndex}, with parent :{parentIndex}, leftNode : {leftNodeIndex}, rightNode: {rightNodeIndex}, mortonCode : {mortonCode}";
        }

        //Morton Code Debug字符串
        public string CodeToString()
        {
            var code = this.mortonCode;
            var res = "";
            var count = 0;
            for (int i = 31; i >= 0; i--)
            {
                var bit = (code >> i) & 0b01;
                res += bit.ToString();
                count++;
                if (count > 7)
                {
                    count = 0;
                    res += " ";
                }
            }
            return res;
        }
    }
}
